package com.hns.can.master.service;

import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.common.aop.sedate.SedateAnnotation;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.master.entity.BaseLineInfoEntity;
import com.hns.can.master.vo.*;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * 车队长首页service
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Service
public class HomeCaptainWholeService extends CommonService {

	@Autowired
	private FillCodeDscValue fillCodeDscValue;

	@Autowired
	private TendencyChartsDataTools tendencyChartsDataTools;	//趋势图补充数据工具

	/*
	 * 首页部分
	 */

	/**
	 * 获取首页指标
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked"})
	@LeftOrgAnnotation
	@Transactional
	public List<HomeCaptainMainVo> getHomeIndex(HomePageParamVo params) {
		List<HomeCaptainMainVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getHomeIndex", params), HomeCaptainMainVo.class);
		return list;
	}


	/**
	 * 获取首页对比指标
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("unchecked")
	@LeftOrgAnnotation
	@Transactional
	public List<HomeCaptainMainVo> getCompareIndex(HomePageParamVo params) {
		List<HomeCaptainMainVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getLastHomeIndex", params), HomeCaptainMainVo.class);
		return list;
	}

	/**
	 * 获取线路下所有车辆最后位置
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@LeftOrgAnnotation
    @SedateAnnotation
	@Transactional
	public List getLineCarLastPosition(HomePageParamVo params) throws Exception {
		List<HomeCaptainMainMapVo> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getLineCarLastPosition", params), HomeCaptainMainMapVo.class);
		return list;
	}



    public HomeCaptainMainMapVo getRecentlyWarn(HomePageParamVo params) throws Exception {
        HomeCaptainMainMapVo vo = new HomeCaptainMainMapVo();
        params.setWarnType(1);
        List<HomeCaptainMainMapVo> mftList = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getLineRecentlyWarn", params), HomeCaptainMainMapVo.class);
        if(PubUtil.isNotEmpty(mftList)){
            vo.setMftTypeName(mftList.get(0).getMftTypeName());
            vo.setMftOccurTime(mftList.get(0).getMftOccurTime());
        }
        params.setWarnType(2);
        List<HomeCaptainMainMapVo> bhvList = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getLineRecentlyWarn", params), HomeCaptainMainMapVo.class);
        if(PubUtil.isNotEmpty(bhvList)){
            vo.setBhvDsc(bhvList.get(0).getBhvDsc());
            vo.setSctBhvBeginTime(bhvList.get(0).getSctBhvBeginTime());
        }
        params.setWarnType(3);
        List<HomeCaptainMainMapVo> spdList = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getLineRecentlyWarn", params), HomeCaptainMainMapVo.class);
        if(PubUtil.isNotEmpty(spdList)){
            vo.setWarnType(spdList.get(0).getWarnType());
            vo.setSpdingBeginTime(spdList.get(0).getSpdingBeginTime());
        }
        return vo;
    }


	/**
	 * 获取线路下车辆指定时间内报警信息
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@LeftOrgAnnotation
	@Transactional
	public List getRecentWarnInfo(HomePageParamVo params) throws Exception {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getRecentWarnInfo", params), HomeCaptainMainMapVo.class);
		return list;
	}

	/*
	 * 安全管理部分
	 */


	/**
	 * 获取故障排名
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@LeftOrgAnnotation
	@Transactional
	public List getFaultsRanking(HomePageParamVo params) throws Exception {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getFaultsRanking", params), HomeCaptainFaultsRankingVo.class);
		return list;
	}


	/**
	 * 线路平均故障数
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("unchecked")
	@LeftOrgAnnotation
	@Transactional
	public List<HomeCaptainFaultsRankingVo> getFaultsRankCount(HomePageParamVo params) {
		List<HomeCaptainFaultsRankingVo> list = PubUtil.convertListMapToObject(
				this.sqlMapDao.selectList("homeCaptain.getFaultsRankCount", params), HomeCaptainFaultsRankingVo.class);
		return list;
	}

	/**
	 * 故障和不安全行为饼图数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("unchecked")
	@LeftOrgAnnotation
	@Transactional
	public List<HomeCaptainFaultRankChartVo> harmfulTypeRanking(HomePageParamVo params) {
		List<HomeCaptainFaultRankChartVo> list = PubUtil.convertListMapToObject(
				this.sqlMapDao.selectList("homeCaptain.harmfulTypeRanking", params), HomeCaptainFaultRankChartVo.class);
		return list;

	}

	/*
	 * 驾驶员管理
	 */

	/**
	 * 车队长首页驾驶员排名（前五名，后五名）
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@LeftOrgAnnotation
	@Transactional
	public List getDriverUnsafeRanking(HomePageParamVo params) throws Exception {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getDriverUnsafeRanking", params), HomeCaptainUnsafeRankingVo.class);
		return list;
	}


	/**
	 * 车队长首页驾驶员平均数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("unchecked")
	@LeftOrgAnnotation
	@Transactional
	public List<HomeCaptainUnsafeRankCountVo> getDriverUnsafeRankCount(HomePageParamVo params) {
		List<HomeCaptainUnsafeRankCountVo> list = PubUtil.convertListMapToObject(
				this.sqlMapDao.selectList("homeCaptain.getDriverUnsafeRankCount", params), HomeCaptainUnsafeRankCountVo.class);
		return list;
	}


	/*
	 *能耗管理部分
	 */

	/**
	 * 查询能耗排名
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@LeftOrgAnnotation
	@Transactional
	public List getEnergyRanking(HomePageParamVo params) throws Exception {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getEnergyRanking", params), HomeCaptainEnergyRankingVo.class);
		return list;
	}


	/**
	 * 查询平均能耗值
	 * @param params
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@LeftOrgAnnotation
	@Transactional
	public List getAvgEnergyVal(HomePageParamVo params) throws Exception {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getAvgEnergyVal", params), HomeCaptainEnergyRankingVo.class);
		return list;
	}


	/**
	 * 能耗趋势值
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@LeftOrgAnnotation
	@Transactional
	public List getEnergyTendencyCharts(HomePageParamVo params) {
		List list = tendencyChartsDataTools.fillEnergySingleTendencyData(this.sqlMapDao.selectList("homeCaptain.getEnergyTendencyCharts", params),params.getDateType());
		list = PubUtil.convertListMapToObject(list, EnergyTendencyVo.class);
		return list;
	}


	/**
	 * 能耗结论数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@LeftOrgAnnotation
	@Transactional
	public List getEnergyConclude(HomePageParamVo params) {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getEnergyConclude", params), HomeCaptainEnergyConcludeVo.class);
		return list;
	}

	/**
	 * 能耗模型饼图数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@LeftOrgAnnotation
	@Transactional
	public List getEnergyStatusPieChart(HomePageParamVo params) {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.energyStatusPieChart", params), HomeCaptainEnergyConcludeVo.class);
		return list;
	}

	/**
	 * 能耗问题和建议部分数据
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@LeftOrgAnnotation
	@Transactional
	public List getEnerMultiInfo(HomePageParamVo params) {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.enerMultiInfo", params), HomeCaptainEnergyConcludeVo.class);
		return list;
	}


	/*
	 * 资金异常
	 */
	/**
	 * 资金异常
	 * @param params
	 * @return
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@LeftOrgAnnotation
	@Transactional
	public Pagination getCaptainFinanceData(HomePageParamVo params){
		Pagination page = this.myBatisPage("homeCaptain.getCaptainFinanceCnt", "homeCaptain.getCaptainFinanceData", params, HomeCaptainMainMapVo.class);
		return page;
	}


	/**
	 * 获取用户线路资源
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List getCaptainLineInfo() {
		HashMap map = new HashMap();
		map.put("userid", this.getCurrentUser().getUser().getId());
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("baseOrgan.getLeftOrg4LineVo", map), LeftOrgVo.class);
		return list;
	}


	@LeftOrgAnnotation
	@Transactional
	public List getLastMiniteWarnList(HomePageParamVo params) {
		List list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("homeCaptain.getLastMiniteWarnList", params), LastMiniteWarnInfoVo.class);
		return list;
	}
}
